package com.ikkong.platform.controller;

import com.ikkong.core.dao.Blade;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ClearShiro;
import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import com.ikkong.platform.meta.intercept.BlogIntercept;
import com.ikkong.platform.model.Blog;
import com.ikkong.platform.model.Product;
import com.ikkong.platform.model.Producttech;
import com.ikkong.platform.model.Producttype;
import com.ikkong.platform.service.BlogService;
import com.ikkong.platform.service.ProductService;
import com.ikkong.platform.service.impl.BlogServiceImpl;
import com.ikkong.platform.service.impl.ProductServiceImpl;
import com.ikkong.system.Util.CommonUtil;
import com.ikkong.system.controller.base.UrlPermissController;
import com.jfinal.kit.PathKit;
import com.jfinal.upload.UploadFile;
import org.beetl.ext.fn.Print;
import org.beetl.sql.core.kit.StringKit;

import java.io.File;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Generated by Blade.
 * 2016-10-08 15:59:45
 */
public class APIController extends UrlPermissController {
	private static String CODE = "blog";
	private static String LIST_SOURCE = "Product.list";
	private static String BASE_PATH = "/platform/internet/";
	
	ProductService service = new ProductServiceImpl();

	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "index.html");
	}

    public void imgupload() {
        String webRootPath = PathKit.getWebRootPath();
        String uploadpath = webRootPath + "/upload/";
        UploadFile file = getFile();
        String name =  file.getParameterName();
        File nf = new File(uploadpath+name+".png");
        if(nf.exists()){
            nf.delete();
        }
        file.getFile().renameTo(new File(uploadpath + name + ".png"));
        render(BASE_PATH + "../changeimg/changeimg.html");
    }

	public void mp() {
        List<Producttype> type = Blade.create(Producttype.class).findBy("p_id = 1", Producttype.class);
        List<Producttech> tech = Blade.create(Producttech.class).findBy("p_id = 1", Producttech.class);
        setAttr("type",type);
        setAttr("tech",tech);
        setAttr("code", CODE);
		render(BASE_PATH + "mp/mp.html");
	}




	public void bgj() {
        List<Producttype> type = Blade.create(Producttype.class).findBy("p_id = 2", Producttype.class);
        List<Producttech> tech = Blade.create(Producttech.class).findBy("p_id = 2", Producttech.class);
        setAttr("type",type);
        setAttr("tech",tech);
		setAttr("code", CODE);
		render(BASE_PATH + "/bgj/bgj.html");
	}

    public void hc() {
        List<Producttype> type = Blade.create(Producttype.class).findBy("p_id = 4", Producttype.class);
        List<Producttech> tech = Blade.create(Producttech.class).findBy("p_id = 4", Producttech.class);
        setAttr("type",type);
        setAttr("tech",tech);
        setAttr("code", CODE);
        render(BASE_PATH + "/hc/hc.html");
    }


    public void ft() {
        List<Producttype> type = Blade.create(Producttype.class).findBy("p_id = 3", Producttype.class);
        List<Producttech> tech = Blade.create(Producttech.class).findBy("p_id = 3", Producttech.class);
        setAttr("type",type);
        setAttr("tech",tech);
        setAttr("code", CODE);
        render(BASE_PATH + "/hb/ft.html");
    }

    public void std() {
        setAttr("code", CODE);
        render(BASE_PATH + "/hb/std.html");
    }

    public void xf() {
        setAttr("code", CODE);
        render(BASE_PATH + "/hb/xf.html");
    }

    public void wtld() {
        setAttr("code", CODE);
        render(BASE_PATH + "/hb/wtld.html");
    }

    public void pvccard() {
        setAttr("code", CODE);
        render(BASE_PATH + "/hb/pvccard.html");
    }

	public void pvc() {
		setAttr("code", CODE);
		render(BASE_PATH + "/pvc/pvc.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "blog_add.html");
	}


	public void list() {
		Object grid = paginate(LIST_SOURCE,new BlogIntercept());
		renderJson(grid);
	}

    public void subbgj() {
        String paper = getPara("Paper");
        Integer longSideLength = getParaToInt("LongSideLength");
        Integer shortSideLength = getParaToInt("ShortSideLength");
        Integer Num = getParaToInt("Num");
        String checkids = getPara("aa");

        //如果长边、短边太小，强制模切
        if(longSideLength<90 || shortSideLength<54){
            checkids = 3 + "";
        }
        //计算面积
        BigDecimal l = new BigDecimal(longSideLength);
        BigDecimal s = new BigDecimal(shortSideLength);
        BigDecimal area = l.multiply(s).divide(new BigDecimal(100));


        Producttype type = Blade.create(Producttype.class).findById(paper);
        String typename = type.getName();
        Integer f_num = type.getF_num();
        BigDecimal f_prise = type.getF_prise();
        BigDecimal l_prise = type.getL_prise();
        BigDecimal f_cost = type.getF_cost();
        BigDecimal l_cost = type.getL_cost();
        BigDecimal num = new BigDecimal(Num);
        BigDecimal fn = new BigDecimal(f_num);
        BigDecimal price1 =  Num - f_num>0?(num.subtract(fn)).multiply(l_prise).add(fn.multiply(f_prise)):num.multiply(f_prise).multiply(area);//材料费
        BigDecimal price1c =  Num - f_num>0?(num.subtract(fn)).multiply(l_cost).add(fn.multiply(f_cost)):num.multiply(f_cost).multiply(area);//材料费成本


        List<String> strings = Arrays.asList(checkids.split(","));
        BigDecimal price2 =  new BigDecimal(0);
        BigDecimal price2c =  new BigDecimal(0);
        String gyname = "";
        for(String ss:strings){
            if(StringKit.isNotBlank(ss)){
                Producttech tech = Blade.create(Producttech.class).findById(ss);
                Integer f_num1 = tech.getF_num();
                BigDecimal fp = tech.getF_prise();
                BigDecimal lp = tech.getL_prise();
                BigDecimal fc = tech.getF_cost();
                BigDecimal lc = tech.getL_cost();
                BigDecimal fn1 = new BigDecimal(f_num1);
                BigDecimal temp  =  Num - f_num1>0?(num.subtract(fn1)).multiply(lp).add(fn1.multiply(fp)):num.multiply(fp);//工艺费
                BigDecimal tempc  =  Num - f_num1>0?(num.subtract(fn1)).multiply(lc).add(fn1.multiply(fc)):num.multiply(fc);//工艺费成本

                price2 = price2.add(temp);
                price2c = price2c.add(tempc);
                gyname = gyname + "_" +  tech.getName();
            }
        }

        BigDecimal price = price1.add(price2).setScale(2,BigDecimal.ROUND_HALF_UP);
        BigDecimal pricec = price1c.add(price2c).setScale(2,BigDecimal.ROUND_HALF_UP);

        String res = "【不干胶】_材料"+typename + gyname +  "_数量"+Num +  "_价格【" + price+"】";
        String cost = res + "_【成本" + pricec+"】";
        Map map = new HashMap<>();
        map.put("data",res);
        if(ShiroKit.getUser()!=null){
            map.put("data",cost);
        }
        renderJson("data",map);



    }




    public void subft() {
        String paper = getPara("Paper");
        Integer printSide = getParaToInt("PrintSide");
        Integer Num = getParaToInt("Num");
        String checkids = getPara("aa");



        Producttype type = Blade.create(Producttype.class).findById(paper);
        String typename = type.getName();
        Integer f_num = type.getF_num();
        BigDecimal f_prise = type.getF_prise();
        BigDecimal l_prise = type.getL_prise();
        BigDecimal f_cost = type.getF_cost();
        BigDecimal l_cost = type.getL_cost();
        BigDecimal num = new BigDecimal(Num);
        BigDecimal fn = new BigDecimal(f_num);
        BigDecimal price1 =  Num - f_num>0?(num.subtract(fn)).multiply(l_prise).add(fn.multiply(f_prise)):num.multiply(f_prise);//材料费
        BigDecimal price1c =  Num - f_num>0?(num.subtract(fn)).multiply(l_cost).add(fn.multiply(f_cost)):num.multiply(f_cost);//材料费成本


        List<String> strings = Arrays.asList(checkids.split(","));
        BigDecimal price2 =  new BigDecimal(0);
        BigDecimal price2c =  new BigDecimal(0);
        String gyname = "";
        for(String ss:strings){
            if(StringKit.isNotBlank(ss)){
                Producttech tech = Blade.create(Producttech.class).findById(ss);
                Integer f_num1 = tech.getF_num();
                BigDecimal fp = tech.getF_prise();
                BigDecimal lp = tech.getL_prise();
                BigDecimal fc = tech.getF_cost();
                BigDecimal lc = tech.getL_cost();
                BigDecimal fn1 = new BigDecimal(f_num1);
                BigDecimal temp  =  Num - f_num1>0?(num.subtract(fn1)).multiply(lp).add(fn1.multiply(fp)):num.multiply(fp);//材料费
                BigDecimal tempc  =  Num - f_num1>0?(num.subtract(fn1)).multiply(lc).add(fn1.multiply(fc)):num.multiply(fc);//材料费

                price2 = price2.add(temp);
                price2c = price2c.add(tempc);
                gyname = gyname + "_" +  tech.getName();
            }
        }

        BigDecimal price = price1.add(price2).setScale(2);
        BigDecimal pricec = price1c.add(price2c).setScale(2);

        String res = "【封套】_"+typename + gyname +  "_数量"+Num +  "_价格【" + price+"】";
        if(ShiroKit.getUser()!=null){
            res += "_【成本"+pricec+"】";
        }
        Map map = new HashMap<>();
        map.put("data",res);
        renderJson("data",map);



    }

	public void submp(){
        Integer paper = getParaToInt("Paper", 0);
        Integer longSideLength = getParaToInt("LongSideLength", 0);
        Integer ShortSideLength = getParaToInt("ShortSideLength", 0);
        Integer StyleNum = getParaToInt("StyleNum", 0);//款式数量
        Integer Num = getParaToInt("Num", 0);
        Integer PrintSide = getParaToInt("PrintSide", 0);//单面是1，双面是2
//        Integer Crafts = getParaToInt("Crafts", 0);

       BigDecimal price = new BigDecimal(0);
        BigDecimal pricec = new BigDecimal(0);

        //选中的工艺
        String checkids = getPara("aa");

        int jisuan = CommonUtil.jisuan(longSideLength, ShortSideLength);//占用多少名片位
        Producttype type = Blade.create(Producttype.class).findById(paper);
        String typename = type.getName();
        Integer f_num = type.getF_num();
        BigDecimal f_prise = type.getF_prise();
        BigDecimal l_prise = type.getL_prise();
        BigDecimal f_cost = type.getF_cost();
        BigDecimal l_cost = type.getL_cost();
        BigDecimal num = new BigDecimal(Num);
        BigDecimal fn = new BigDecimal(f_num);
        BigDecimal price1 =  Num - f_num>0?(num.subtract(fn)).multiply(l_prise).add(fn.multiply(f_prise)):num.multiply(f_prise);//材料费
        BigDecimal price1c =  Num - f_num>0?(num.subtract(fn)).multiply(l_cost).add(fn.multiply(f_cost)):num.multiply(f_cost);//材料费成本

        //工艺费
        List<String> strings = Arrays.asList(checkids.split(","));
        BigDecimal price2 =  new BigDecimal(0);
        BigDecimal price2c =  new BigDecimal(0);
        String gyname = "";
        for(String ss:strings){
            if(StringKit.isNotBlank(ss)){
                Producttech tech = Blade.create(Producttech.class).findById(ss);
                Integer f_num1 = tech.getF_num();
                BigDecimal fp = tech.getF_prise();
                BigDecimal lp = tech.getL_prise();
                BigDecimal fc = tech.getF_cost();
                BigDecimal lc = tech.getL_cost();
                BigDecimal fn1 = new BigDecimal(f_num1);
                BigDecimal temp  =  Num - f_num1>0?(num.subtract(fn1)).multiply(lp).add(fn1.multiply(fp)):num.multiply(fp);//工艺费
                BigDecimal tempc  =  Num - f_num1>0?(num.subtract(fn1)).multiply(lc).add(fn1.multiply(fc)):num.multiply(fc);//工艺费成本
                price2 = price2.add(temp);
                price2c = price2c.add(tempc);
                gyname = gyname + "_" +  tech.getName();
            }
        }
        price = price1.add(price2).multiply(new BigDecimal(PrintSide)).multiply(new BigDecimal(StyleNum)).multiply(new BigDecimal(jisuan));
        pricec = price1c.add(price2c).multiply(new BigDecimal(PrintSide)).multiply(new BigDecimal(StyleNum)).multiply(new BigDecimal(jisuan));

        String pagenum = PrintSide == 1?"_单面_":"_双面_";
        String res = "【名片】_材料"+typename + gyname + pagenum + "数量"+Num + "_款数"+StyleNum + "_价格【" + price+"】";
        String cost = "【成本】【" + pricec+"】";
        Map map = new HashMap<>();
        map.put("data",res);
        if(ShiroKit.getUser()!=null){
            map.put("cost",cost);
        }


        renderJson("data",map);




    }




    public void subhc(){


        Integer paper = getParaToInt("Paper", 0);
        Integer num = getParaToInt("Num", 0);
        Integer kai = getParaToInt("PrintSide", 2);//开数
//        Integer Crafts = getParaToInt("Crafts", 0);
        Integer zhang = getParaToInt("StyleNum", 0);//一张的成品数


        Producttype ptype = Blade.create(Producttype.class).findById(paper);
        BigDecimal f_prise = ptype.getF_prise();
        BigDecimal f_cost = ptype.getF_cost();



        double danjia = f_prise.doubleValue();;//单价
        double chengben = f_cost.doubleValue();;//单价
//	    int kai = 16;//16开
//	    int num = 50000;//印刷50000份
	    int jiqi = 8*2;//8开机器
        double money = 0.0;
        double moneyc = 0.0;
        int jqchoose = 0;
        for(int i = 0 ;i<3;i++){
            jiqi = jiqi/2;
            if(jiqi>kai){
                continue;
            }
            double temp = CommonUtil.money(danjia, kai, num, zhang, jiqi);
            double tempc = CommonUtil.money(chengben, kai, num, zhang, jiqi);
            if(temp < money || money == 0.0){
                money = temp;
                jqchoose = jiqi;
            }
            if(tempc < moneyc || moneyc == 0.0){
                moneyc = tempc;
            }

        }
        DecimalFormat df   = new DecimalFormat("######0.00");
        String smoney = df.format(money);
        String smoneyc = df.format(moneyc);
        smoney = "【材料】_"+ptype.getName()+"_"+kai+"开_"+"_数量"+num+"_一次能印成品数"+zhang + "【价格】"+smoney;
        if(ShiroKit.getUser()!=null){
            smoney += "_【成本"+smoneyc+"】_使用【"+jqchoose+"】开机器";
        }
        Map map = new HashMap();
        map.put("data",smoney);
        renderJson(map);

    }

}
